<template>
  <div>
    <div class="front-notice"><i class="el-icon-bell" style="margin-right: 2px"></i>公告：{{ top }}</div>
    <!--头部-->
    <div class="front-header">
      <div class="front-header-left">
        <img src="@/assets/imgs/logo.png" alt="">
        <div class="title">博客论坛平台</div>
      </div>
      <div class="front-header-center">
        <div class="front-header-nav">
          <el-menu :default-active="$route.path" mode="horizontal" router>
						<el-menu-item index="/weblog/home">首页</el-menu-item>
            <el-menu-item index="/weblog/activity">活动中心</el-menu-item>
						<el-menu-item index="/weblog/person" v-if="user.username !== undefined">个人中心</el-menu-item>
          </el-menu>
        </div>
      </div>
      <div v-if="this.$route.path !== '/weblog/person'">
        <el-input style="width: 260px; margin-right: 10px" placeholder="请输入关键字搜索" v-model="title" clearable></el-input>
        <el-button type="success" @click="goSearch">搜 索</el-button>
      </div>
      <div class="front-header-right">
        <div v-if="!user.username">
          <el-button @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown>
            <div class="front-header-dropdown">
              <img :src="user.avatar" alt="">
              <div style="margin-left: 10px; color: #fff;">
                <span>{{ user.name }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
              </div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div style="text-decoration: none" @click="go('/weblog/person')">个人中心</div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div style="text-decoration: none" @click="logout">退出</div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <!--主体-->
    <div class="main-body">
      <router-view ref="child" @update:user="updateUser" />
    </div>
    <Footer />
  </div>

</template>

<script>

import router from "@/router";

export default {
  name: "FrontLayout",

  data () {
    return {
      top: '',
      notice: [],
      user: JSON.parse(localStorage.getItem("weblog") || '{}'),
      title: this.$route.query.title,
    }
  },

  mounted() {
    console.log('user', this.user.username)
    this.loadNotice()
  },
  methods: {
    router() {
      return router
    },
    loadNotice() {
      this.$request.get('/notice/selectAll').then(res => {
        this.notice = res.data
        let i = 0
        if (this.notice && this.notice.length) {
          this.top = this.notice[0].content
          setInterval(() => {
            this.top = this.notice[i].content
            i++
            if (i === this.notice.length) {
              i = 0
            }
          }, 2500)
        }
      })
    },
    // 搜索博客
    goSearch() {
      if (!this.title) {
        this.$message.error('请输入搜索关键字')
        return
      }
      //window.open('/weblog/search?title' + this.title)    // 跳转新页面展示搜索结果
      location.href = '/weblog/search?title=' + this.title
    },
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('weblog') || '{}')   // 重新获取下用户的最新信息
    },
    // 退出登录
    async logout() {
      localStorage.removeItem("weblog");
      await this.$router.push("/login");
      //this.$router.push("/weblog/home");
      this.$message.success("退出成功");
    },
    go(path) {
      this.$router.push({path: path})
    }
  }

}
</script>

<style scoped>
  @import "@/assets/css/front.css";
</style>